package com.jiang.jixiang.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jiang.jixiang.R;
import com.jiang.jixiang.api.ApiUrl;
import com.jiang.jixiang.bean.UserInfoBean;
import com.jiang.jixiang.databinding.ActivityUsercenterBinding;
import com.jiang.jixiang.network.CommonalityModel;
import com.jiang.jixiang.network.NetWorkListener;
import com.jiang.jixiang.network.okHttpModel;
import com.jiang.jixiang.tools.PubDiaUtils;
import com.jiang.jixiang.ui.BillingActivity;
import com.jiang.jixiang.ui.ContactActivity;
import com.jiang.jixiang.ui.LanguageActivity;
import com.jiang.jixiang.ui.MalfunctionActivity;
import com.jiang.jixiang.ui.MoreFunctionActivity;
import com.jiang.jixiang.ui.MyOrderActivity;
import com.jiang.jixiang.ui.UserInfoActivity;
import com.jiang.jixiang.ui.UsingHelpActivity;
import com.ws.universal.tools.base.BaseFragment;
import com.ws.universal.tools.eventbus.BaseBusData;
import com.ws.universal.tools.utils.AppUtils;
import com.ws.universal.tools.utils.GsonUtils;
import com.ws.universal.tools.utils.StringUtils;
import com.ws.universal.tools.utils.ToastUtils;
import com.ws.universal.tools.utils.glide.GlideUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class UserCenterFragment extends BaseFragment implements NetWorkListener {
    private ActivityUsercenterBinding binding;
    private Bundle bundle;
    private Intent intent;


    @Override
    public View getRootView(LayoutInflater inflater, ViewGroup container) {
        binding = ActivityUsercenterBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void initView() {
        binding.titleLeftBtn.setVisibility(View.GONE);
        binding.titleTextTv.setText(R.string.personal_center);

        binding.titleRightBtn.setOnClickListener(view -> {
            Intent intent = new Intent(getContext(), LanguageActivity.class);
            intent.putExtra("type", 2);
            startActivity(intent);
        });

        //个人信息
        binding.imgMeHead.setOnClickListener(view -> {
            pageJump(UserInfoActivity.class);
        });

        //订单列表
        binding.rlMeOrder.setOnClickListener(view -> {

            pageJump(MyOrderActivity.class);
        });
        //账单记录
        binding.rlBilling.setOnClickListener(view -> {

            pageJump(BillingActivity.class);
        });


        //使用帮助
        binding.llUsingHelp.setOnClickListener(view -> {

            pageJump(UsingHelpActivity.class);
        });

        //使用帮助
        binding.rlMalfunction.setOnClickListener(view -> {

            pageJump(MalfunctionActivity.class);
        });
        //更多功能
        binding.rlMoreFunction.setOnClickListener(view -> {

            pageJump(MoreFunctionActivity.class);
        });

        //联系方式
        binding.rlContact.setOnClickListener(view -> {

            pageJump(ContactActivity.class);
        });


        //
        binding.tvUserName.setOnClickListener(view -> {
            pageJump(UserInfoActivity.class);
        });

        binding.tvVersion.setText(StringUtils.getString("v", AppUtils.getAppVersionName()));

        //loadData();
    }

    @Override
    public void autoData() {

    }


    @Subscribe(threadMode = ThreadMode.MAIN)  //3.0之后，需要加注解
    public void onEventMainThread(BaseBusData data) {

        /*switch (data.action) {
            case BusCode.updateUserInfo:
            case BusCode.updateUserInfoAvatar:
                loadData();
                break;

        }*/
    }

    public void loadData() {
        //showProgressDialog(getActivity(),false);
        Map<String, String> params = new HashMap<>();
        okHttpModel.post(ApiUrl.getUserInfo_Api, params, ApiUrl.getUserInfo_Api_ID, this);
    }

    @Override
    public void onSucceed(JSONObject jsonData, int id, CommonalityModel commonality) {
        stopProgressDialog();
        JSONObject data = jsonData.optJSONObject("data");
        switch (id) {
            case ApiUrl.getUserInfo_Api_ID:
                stopProgressDialog();
                UserInfoBean userInfoBean = GsonUtils.parseJObject(data.toString(), UserInfoBean.class);
                PubDiaUtils.getInstance().saveUserInfo(data.toString());
                GlideUtils.CreateImageRound(userInfoBean.getAvatar(), binding.imgMeHead, R.mipmap.icon_def_head, R.mipmap.icon_def_head);
                binding.tvUserName.setText(userInfoBean.getNick());
                binding.tvMemberId.setText(StringUtils.getString(getString(R.string.id_number), userInfoBean.getMember_id()));
                break;


        }


    }

    @Override
    public void onFail(JSONObject jsonData, int id, CommonalityModel commonality) {
        stopProgressDialog();
        ToastUtils.showShort(commonality.getMessage());
    }

    @Override
    public void onError(int id, Exception e) {
        stopProgressDialog();
        ToastUtils.showShort(e.getMessage());
    }


}
